package cn.bugDance.domain.activity.service.quota;

import cn.bugDance.domain.activity.model.entity.ActivityAccountEntity;
import cn.bugDance.domain.activity.model.entity.ActivitySkuEntity;
import cn.bugDance.domain.activity.model.entity.DeliverOrderEntity;
import cn.bugDance.domain.activity.model.valobj.ActivitySkuStockSubtractionJobData;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Console;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RaffleActivityQuotaService implements IRaffleActivityQuotaService {

    @Resource
    private IActivityRepository activityRepository;

    private final SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public ActivitySkuStockSubtractionJobData takeJobQueue(Long sku) {
        return activityRepository.takeJobQueue(sku);
    }

    @Override
    public void activitySkuStockSubtractQuota(Long sku) {
        activityRepository.activitySkuStockSubtractQuota(sku);
    }

    @Override
    public Integer queryUserDayCount(String userId, Long activityId) {
        String day = dayFormat.format(new Date());
        return activityRepository.queryUserDayCount(userId, activityId, day);
    }

    @Override
    public Integer queryUserTotalCountSurplus(String userId, Long activityId) {
        return activityRepository.queryUserTotalCountSurplus(userId, activityId);
    }

    @Override
    public Integer queryUserMonthCountSurplus(String userId, Long activityId, String month) {
        return activityRepository.queryUserMonthCountSurplus(userId, activityId, month);
    }

    @Override
    public Integer queryUserDayCountSurplus(String userId, Long activityId, String day) {
        return activityRepository.queryUserDayCountSurplus(userId, activityId, day);
    }

    @Override
    public Integer queryRaffleActivityAccountPartakeCount(String userId, Long activityId) {
        return activityRepository.queryRaffleActivityAccountPartakeCount(userId, activityId);
    }

    @Override
    public ActivityAccountEntity queryActivityAccount(String userId, Long activityId) {
        return activityRepository.queryActivityAccountByUserIdAndActivityId(userId, activityId);
    }

    @Override
    public List<Long> queryActivitySkuList() {
        return activityRepository.queryActivitySkuAll();
    }

    @Override
    public void clearActivitySkuJobQueue(Long sku) {
        activityRepository.clearActivitySkuJobQueue(sku);
    }

    @Override
    public void updateActivitySkuStockOver(Long sku) {
        activityRepository.updateActivitySkuStockOver(sku);
    }

    @Override
    public void updateCreditPayOrder(DeliverOrderEntity deliverOrderEntity) {
        activityRepository.updateCreditPayOrder(deliverOrderEntity);
    }

    @Override
    public List<ActivitySkuEntity> queryRechargeSku(Long activityId) {
        List<ActivitySkuEntity> activitySkuEntities = activityRepository.queryActivitySkuByActivityId(activityId);
        for (ActivitySkuEntity activitySkuEntity : activitySkuEntities) {
            Integer stockSurplusCount = activityRepository.queryStockSurplusCount(activitySkuEntity.getSku());
            activitySkuEntity.setStockCountSurplus(stockSurplusCount);
        }
        return activitySkuEntities;
    }

    @Override
    public ActivitySkuEntity queryActivitySku(Long sku) {
        return activityRepository.queryActivitySkuBySku(sku);
    }
}
